package com.lyl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyl.common.BaseContext;
import com.lyl.common.R;
import com.lyl.entity.ShoppingCart;
import com.lyl.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 李亚龙
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 将菜品或者套餐加入购物车中
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("将菜品或者套餐加入购物车中：{}",shoppingCart);
        //设置用户id，指定当前是哪个用户点的餐
        Long currentUserId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentUserId);
        //查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentUserId);
        if (dishId != null){
            //说明是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //说明是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (cartServiceOne != null){
            //如果已经有了，在原先number加一
            cartServiceOne.setNumber(cartServiceOne.getNumber()+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //如果没有存在，则添加至购物车，数量默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;//这里方便给前端返回
        }

        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车");

        Long currentUserId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentUserId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);

        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        log.info("清空购物车");
        Long currentUserId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentUserId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("减少订单{}数量",shoppingCart);
        //获取用户id
        Long currentUserId = BaseContext.getCurrentId();
        //这里传入了dishId或者setmealId
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentUserId);
        ShoppingCart cartServiceOne = null;
        if (dishId != null){
            //说明传入的是dishId

            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            cartServiceOne = shoppingCartService.getOne(queryWrapper);
        }else{
            //说明传入的setmealId
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            cartServiceOne = shoppingCartService.getOne(queryWrapper);
        }

        //先判断数量是否为1，如果大于1，则数量减一，如果等于1，则直接删除该订单
        if (cartServiceOne.getNumber() > 1){
            cartServiceOne.setNumber(cartServiceOne.getNumber() - 1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            cartServiceOne.setNumber(null);//这里设置null，是为了前端不再显示数量
            shoppingCartService.removeById(cartServiceOne);
        }

        return R.success(cartServiceOne);
    }

}
